package com.cskaoyan.wordmemorize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cskaoyan.wordmemorize.converter.VocCategoryConverter;
import com.cskaoyan.wordmemorize.dao.entity.VocBookDO;
import com.cskaoyan.wordmemorize.dao.entity.VocCategoryDO;
import com.cskaoyan.wordmemorize.dao.mapper.VocCategoryMapper;
import com.cskaoyan.wordmemorize.dto.admin.DeleteBookCategoryOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.VocCategoryDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.VocCategoryCommand;
import com.cskaoyan.wordmemorize.service.VocCategoryService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class VocCategoryServiceImpl extends ServiceImpl<VocCategoryMapper, VocCategoryDO> implements VocCategoryService {

    @Resource
    private VocCategoryMapper vocCategoryMapper;
    @Resource
    VocCategoryConverter  vocCategoryConverter;

    @Override
    public PageDTO<VocCategoryDTO> getBookCategories(PageRequest pageQuery) {

        List<VocCategoryDO> records;
        long total = 0L;

        // 1. 获取参数,没有参数
        if (pageQuery.getPageNum() == null){
            List<VocCategoryDO> vocCategoryDOS = vocCategoryMapper.selectList(null);
            records = vocCategoryDOS;
            total = vocCategoryDOS.size();

        }else {
            // 2. 有参数分页逻辑， 如果有条件，加条件，没有则搜索全部
            Page<VocCategoryDO> page = new Page<>(pageQuery.getPageNum(), pageQuery.getCount());
            QueryWrapper<VocCategoryDO> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(pageQuery.getKeyword() != null,"category_name",pageQuery.getKeyword());
            IPage<VocCategoryDO> vocCategoryDOIPage = vocCategoryMapper.selectPage(page, queryWrapper);
            records = vocCategoryDOIPage.getRecords();
            total = vocCategoryDOIPage.getTotal();
        }

        // 3. 返回前端对象
        PageDTO<VocCategoryDTO> vocCategoryDTOPageDTO = vocCategoryConverter.bookCategoryList2Page(records, total);
        return vocCategoryDTOPageDTO;
    }

    @Override
    public VocCategoryDTO getBookCategory(Long categoryId) {

        // 1. 根据id查询
        VocCategoryDO vocCategoryDO = vocCategoryMapper.selectById(categoryId);
        if (vocCategoryDO != null) {
            VocCategoryDTO vocCategoryDTO = vocCategoryConverter.bookCategoryEntity2DTO(vocCategoryDO);
            return vocCategoryDTO;
        }
        return null;
    }

    @Override
    @Transactional
    public Long addBookCategory(VocCategoryCommand command) {
        // 1. 添加时注意 id 没有值，赋值
        command.setId(null);
        // 2. 转化为 DO 对象 ，添加
        VocCategoryDO vocCategoryDO = vocCategoryConverter.bookCategoryCommand2Entity(command);
        int insert = vocCategoryMapper.insert(vocCategoryDO);
        if (insert > 0){
            return 1L;
        }
        return 0L;
    }

    @Override
    @Transactional
    public void updateBookCategory(VocCategoryCommand command) {
        VocCategoryDO vocCategoryDO = vocCategoryConverter.bookCategoryCommand2Entity(command);
        vocCategoryMapper.updateById(vocCategoryDO);
    }

    @Override
    @Transactional
    public DeleteBookCategoryOperateDTO deleteBookCategory(Long categoryId) {

        // 1. 先查找 ，有无书籍
        VocCategoryDO vocCategoryDO = vocCategoryMapper.selectById(categoryId);
        if (vocCategoryDO == null){
            return null;
        }
        // 2. 有就删除
        vocCategoryMapper.deleteById(categoryId);
        DeleteBookCategoryOperateDTO deleteBookCategoryOperateDTO = vocCategoryConverter.bookCategoryDOToDeleteCategoryDTO(vocCategoryDO);
        return deleteBookCategoryOperateDTO;
    }

    @Override
    @Transactional
    public List<DeleteBookCategoryOperateDTO> deleteBookCategories(BatchDeleteCommand command) {

        // 用iserve附带功能，比mapper方便
        List<VocCategoryDO> vocCategoryDOS = listByIds(command.getIds());
        if (vocCategoryDOS == null){
            return null;
        }
        removeByIds(command.getIds());
        List<DeleteBookCategoryOperateDTO> deleteBookCategoryOperateDTOList = vocCategoryConverter.bookCategoryDOsToDeleteCategoryDTOs(vocCategoryDOS);
        return deleteBookCategoryOperateDTOList;
    }
}
